/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.tag;

import com.apt.bean.User;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class AccountDisableTag extends SimpleTagSupport {

    private String page;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {
            int cursor1;
            String query = "select * from Accounts where [Account_Status]=0 and Account_Role!='admin'";
            HashMap<Integer, User> hm = GetUser.getListUser(query);
            try {
                if (hm != null && hm.size() != 0) {
                    out.println(Process_Page.divPage(page, hm.size(), 8));
                    int uid_f = 1;
                    int uid_l = 30;

                    try {
                        cursor1 = Integer.parseInt(page);

                    } catch (NumberFormatException ex) {
                        cursor1 = 1;
                    }
                    if (cursor1 > 1) {
                        uid_f = (cursor1 - 1) * 30;
                        uid_l = cursor1 * 30;
                    }

                    int current_page = uid_l / 30;

                    for (int i = uid_f; i < uid_l; i++) {
                        User u = hm.get(i);
                        if (u != null) {

                            StringBuilder sb = new StringBuilder(" <tr>");
//                            sb.append(" <td><input type='checkbox' /></td>");
                            sb.append("<td>" + u.getUser_Name() + "</td>");
                            sb.append("  <td><a href=\"#\" title=\"title\">" + u.getUser_Email() + "t</a></td>");
                            sb.append("   <td>" + u.getDate_Registed() + "</td>");
                            sb.append("   <td>" + u.getDate_Expired() + "</td>");
                            sb.append("    <td>");
                            sb.append("        <!-- Icons -->");
                            sb.append("        <a href=\"Enable_User?user_id=" + u.getAccount_Id() + "&pageno=" + current_page +"&menu=8"+ "\" title=\"Enable\"><img src=\"resources/images/icons/pencil.png\" alt=\"Enabale\" /></a>");
                            sb.append("   </td>");
                            sb.append(" </tr>");

                            out.print(sb.toString());

                        }
                    }

                }


            } catch (IOException ex) {
                Logger.getLogger(ShowUser.class.getName()).log(Level.SEVERE, null, ex);
            }


            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (java.io.IOException ex) {
            throw new JspException("Error in AccountDisableTag tag", ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }
}
